﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Model
{
    /// <summary>
    /// RESPONSABILITY: - project repository
    ///                 - create a new project
    ///                 - add the project to the list of projects
    ///                 - delete a project
    /// </summary>
    public class ProjectChange
    {
        private List<Project> projects=new List<Project>();
        public IEnumerable<Project> GetProjects(List<string> codes = null)
        {
            if (codes == null)
            {
                return projects;
            }

            var result = projects.Where(c => codes.Contains(c.ProjectCode));

            //Console.WriteLine("GetCompanies returns " + result.Count() + " results" );

            return result;
        }

        public Project CreateProject(string projectCode)
        {
            if (string.IsNullOrEmpty(projectCode))
            {
                throw new ArgumentException("The employee cod cannot be null", projectCode);
            }

            Project project = new Project(projectCode);
            this.AddProject(project);
            return project;
        }

        public void AddProject(Project project)
        {
            if (CheckProjectIDExistance(project.ProjectId))
            {
                return;
            }

            if (CheckProjectCodeExistance(project.ProjectCode))
            {
                throw new Exception("A project with the code " + project.ProjectCode + " already exists");
            }

            projects.Add(project);

        }

        private bool CheckProjectCodeExistance(string code)
        {
            var _project = projects.FirstOrDefault(p => p.ProjectCode == code);

            var isFound = _project != null;

            return isFound;
        }

        private bool CheckProjectIDExistance(int id)
        {
            var _project = projects.FirstOrDefault(e => e.ProjectId == id);

            var isFound = _project != null;

            return isFound;
        }

        public void DeleteProject(Project project)
        {
            Project removeProject = projects.FirstOrDefault(p => p.ProjectId == project.ProjectId);
            if (removeProject != null)
            {
                projects.Remove(project);
            }
        }
    }
}
